package de.grosjohann.android.feuerwehr.c2dm;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import de.grosjohann.android.feuerwehr.R;
import de.grosjohann.android.feuerwehr.notification.Notify;
import de.grosjohann.android.feuerwehr.service.CalendarNotificationService;
import de.grosjohann.android.feuerwehr.service.FMSNotificationService;
import de.grosjohann.android.feuerwehr.service.IntentServiceFacade;
import de.grosjohann.android.feuerwehr.service.ThirdPartyRegistrationService;
import de.grosjohann.android.feuerwehr.service.ThirdPartyUnregistrationService;
import de.grosjohann.android.feuerwehr.service.WeatherNotificationService;
import de.grosjohann.android.feuerwehr.util.PrefsUtil;

public class C2DMReceiver
  extends BroadcastReceiver {

  private static final String NAME = "C2DMReceiver";

  private static final String UPDATE_ALL = "all";
  private static final String UPDATE_FMS = "fms";
  private static final String UPDATE_WEATHER = "weather";

  @Override
  public void onReceive(Context ctx, Intent intent) {
    if ("com.google.android.c2dm.intent.REGISTRATION".equals(intent.getAction())) {
      handleRegistration(ctx, intent);
    } else if ("com.google.android.c2dm.intent.RECEIVE".equals(intent.getAction())) {
      handleMessage(ctx, intent);
    }
  }

  private void handleRegistration(Context ctx, Intent intent) {
    String registration = intent.getStringExtra("registration_id");

    if (intent.getStringExtra("error") != null) {
      // Registration failed, should try again later.
      Log.e(NAME, intent.getStringExtra("error"));
      PrefsUtil.setBoolean(ctx, R.string.prefKeyPush, false);

      String msg = ctx.getString(R.string.errC2DMRegistration);
      Notify.getInstance().add(ctx, msg);

    } else if (intent.getStringExtra("unregistered") != null) {
      // unregistration done, new messages from the authorized sender will be rejected

      C2DMRegistration.getInstance(ctx).deleteRegistrationID();

      IntentServiceFacade.startOnce(ctx, ThirdPartyUnregistrationService.class);

    } else if (registration != null) {
      // Send the registration ID to the 3rd party site that is sending the messages.
      // This should be done in a separate thread.
      // When done, remember that all registration is done.

      C2DMRegistration.getInstance(ctx).setRegistrationID(registration);

      IntentServiceFacade.startOnce(ctx, ThirdPartyRegistrationService.class);
    }
  }

  private void handleMessage(Context ctx, Intent intent) {
    String update = intent.getExtras().getString("update");

    if (UPDATE_ALL.equals(update)) {
      IntentServiceFacade.startOnce(ctx, FMSNotificationService.class);
      IntentServiceFacade.startOnce(ctx, WeatherNotificationService.class);
      IntentServiceFacade.startOnce(ctx, CalendarNotificationService.class);

    } else if (UPDATE_FMS.equals(update)) {
      IntentServiceFacade.startOnce(ctx, FMSNotificationService.class);

    } else if (UPDATE_WEATHER.equals(update)) {
      IntentServiceFacade.startOnce(ctx, WeatherNotificationService.class);
    }
  }
}
